/*
 * This file is a part of the RMI Plugin for Eclipse tutorials.
 * Copyright (C) 2002-7 Genady Beryozkin
 */
package display;

/**
 *@author Adam,
 */

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;

import javax.swing.JLabel;


import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.ID3v2_2;



public class RMIServerImpl extends UnicastRemoteObject implements RMIServer {
	
	/*
	 * something must set the name, check for other storages beforehand,
	 * with String[] registryComponents = registry.list();
	 */

	int port = 0;
	private String name;
	private static final long serialVersionUID = 5885886202424414094L;
	private JLabel msg;
	Registry registry;

	public RMIServerImpl(String name) throws RemoteException {
		this.name = name;
	}
	
	
	public RMIServerImpl(String name, JLabel msg) throws RemoteException {
		this.name = name;
		this.msg=msg;
	}

	
	public int getPort() throws RemoteException{
		return port;
	}
	
	public void displayInfo(String s) throws RemoteException{
		System.out.println(s);
//		msg.setText(s);
//		msg.repaint();
	}
	
		
	public void begin() {
		try {
			registry = LocateRegistry.getRegistry();
			registry.rebind(name, new RMIServerImpl(name,msg));
//			registry.rebind("name", new RMIServerImpl("name"));
			port = registry.REGISTRY_PORT;
			System.out.println("started RMI server");
		} catch (RemoteException e) {
			System.err.println("Something happened trying to start RMI");
			e.printStackTrace();
			System.exit(-1);
		}
		System.out.println("The RMI server " + name +" is ready");
//		System.out.println("The RMI server is ready");
	}
	
	public void close(){
		try{
		registry.unbind(name);
		} catch (Exception e){
			e.printStackTrace();
		}
	}
}